package com;

import java.io.IOException;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@SuppressWarnings("serial")
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet{
	User theUser = new User();
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response){
		// Check if the user is logging in
		if (theUser.login(request.getParameter("username"), request.getParameter("password"), request.getSession().getId())){
			try {
				response.sendRedirect("ruleviolations.jsp");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else{
			try {
				// Stay on the Login page if the login info was incorrect
				response.sendRedirect("login.jsp");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response){
		// Check if the user is logging out
		if (request.getParameter("username") == null){
			theUser.logout(request.getSession().getId());
			try {
				// Go to the login page
				response.sendRedirect("login.jsp");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}